﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Titul.Help.WebUI.Controllers
{
    public partial class VersionsController : Controller
    {
        private Storages.VersionStorage _storage;
        public VersionsController()
        {
            _storage = new Storages.VersionStorage();
        }

        [HttpGet]
        public virtual ViewResult Edit(string productSeoUrl, int? id = null)
        {
            if (id != null)
                return View(_storage.Single((int)id));
            else
            {
                return View(new Models.Version
                {
                    ProductSeoUrl = productSeoUrl,
                    SeoUrl = string.Format("{0}-{1}", productSeoUrl, Guid.NewGuid())
                });
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Edit(Models.Version version)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction(MVC.Products.Details(seoUrl: version.ProductSeoUrl));
            }
            else
                return View(version);
        }
    }
}